package com.jzo2o.mall.order.job;

import com.jzo2o.mall.common.enums.RefundStatusEnum;
import com.jzo2o.mall.payment.model.domain.RefundLog;
import com.jzo2o.mall.payment.model.dto.RefundResultResDTO;
import com.jzo2o.mall.payment.service.PayDelegate;
import com.jzo2o.mall.payment.service.RefundLogService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;

/**
 * @author Mr.M
 * @version 1.0
 * @description 退款任务
 * @date 2024/5/26 7:15
 */
@Component
@Slf4j
public class RefundTask {
    @Autowired
    private PayDelegate payDelegate;
    @Autowired
    private RefundLogService refundLogService;

    /**
     * 使用 SpringTask实现每隔10分钟扫描退款日志表，查询近1个小时内退款日志且没有退款成功的记录
     */
    @Scheduled(cron = "0/10 * * * * ?")
    public void refundTask() {
        List<RefundLog> refundLogs = refundLogService.queryUnRefundLogs();
        int size = refundLogs.size();
//      log.info("定时查询退款日志，查询未退款记录{}",size);

        //遍历未退款记录，请求支付服务进行退款，如果退款成功则更新退款日志状态
        refundLogs.forEach(refundLog -> {
            //进行退款
            BigDecimal refundAmount = new BigDecimal(refundLog.getPayPrice()).setScale(2, RoundingMode.HALF_UP);
            RefundResultResDTO refundResult = payDelegate.refund(refundLog.getPaymentReceivableNo(),refundLog.getId(),refundAmount );
            //如果退款成功则更新退款日志
            if (refundResult!=null && refundResult.getRefundStatus().equals(RefundStatusEnum.SUCCESS.getCode())) {
//                refundLogService.updateRefundResult(refundLog.getPaymentReceivableNo(), refundResult.getRefundId(), true);
                refundLogService.updateRefundResult(refundLog.getId(), refundResult.getRefundId(), true);
            }
        });
    }

}
